﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.WindowsAPICodePack.ApplicationServices;

namespace BaseLibrary.Power
{
    
    [TemplatePart(Name = PowerWatcherVisual.C_BatteryGrid, Type = typeof(Grid))]
    [TemplatePart(Name = PowerWatcherVisual.C_BatteryFillRow, Type = typeof(RowDefinition))]
    [TemplatePart(Name = PowerWatcherVisual.C_BatteryEmptyRow, Type = typeof(RowDefinition))]
    [TemplatePart(Name = PowerWatcherVisual.C_PowerWatcher, Type = typeof(PowerWatcher))]
    [TemplatePart(Name = PowerWatcherVisual.C_BatteryPercent, Type = typeof(TextBlock))]
    [TemplatePart(Name = PowerWatcherVisual.C_UspSymbol, Type = typeof(Grid))]
    [TemplatePart(Name = PowerWatcherVisual.C_BatterySymbol, Type = typeof(Grid))]
    [TemplatePart(Name = PowerWatcherVisual.C_UspSymbol, Type = typeof(Grid))]
    [TemplatePart(Name = PowerWatcherVisual.C_BatteryBackground, Type = typeof(Rectangle))]
    public class PowerWatcherVisual : Control
    {
        internal const string C_BatterySymbol = "batterySymbol";
        internal const string C_UspSymbol = "uspSymbol";
        internal const string C_BatteryGrid = "batteryGrid";
        internal const string C_BatteryFillRow = "batteryFillRow";
        internal const string C_BatteryPercent = "batteryPercent";
        internal const string C_PowerWatcher = "powerWatcher";
        internal const string C_BatteryEmptyRow = "batteryEmptyRow";
        internal const string C_BatteryBackground = "batteryBackground";

        Rectangle batteryBackground;
        Grid batteryGrid;
        RowDefinition batteryFillRow;
        RowDefinition batteryEmptyRow;
        TextBlock batteryPercent;
        PowerWatcher powerWatcher;
        Grid batterySymbol;
        Grid uspSymbol;

        public PowerWatcherVisual()
        {
            DefaultStyleKey = typeof(PowerWatcherVisual);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            batteryGrid = GetTemplateChild(C_BatteryGrid) as Grid;
            batteryFillRow = GetTemplateChild(C_BatteryFillRow) as RowDefinition;
            batteryEmptyRow = GetTemplateChild(C_BatteryEmptyRow) as RowDefinition;
            batteryPercent = GetTemplateChild(C_BatteryPercent) as TextBlock;
            
            powerWatcher = GetTemplateChild(C_PowerWatcher) as PowerWatcher;

            uspSymbol = GetTemplateChild(C_UspSymbol) as Grid;
            batterySymbol = GetTemplateChild(C_BatterySymbol) as Grid;
            batteryBackground = GetTemplateChild(C_BatteryBackground) as Rectangle;

            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            powerWatcher.PowerPersonalityChanged += (s, e) =>
            {

            };

            powerWatcher.PowerSourceChanged += (s, e) =>
            {
                CheckPowerSource(e);
            };

            powerWatcher.BatteryLifePercentChanged += (s, e) =>
            {
                double percent = (double) e.BatteryLifePercent / 100.0;
                GridLength glFill = new GridLength(percent, GridUnitType.Star);
                batteryFillRow.Height = glFill;
                
                GridLength glEmpty = new GridLength(1-percent, GridUnitType.Star);
                batteryEmptyRow.Height = glEmpty;

                batteryPercent.Text = e.BatteryLifePercent.ToString() + " %";

                CheckBatteryIsShortTerm();
            };

            CheckPowerSource(new PowerEventArgs());
            CheckBatteryIsShortTerm();
        }

        private void CheckBatteryIsShortTerm()
        {
            if (PowerManager.IsBatteryShortTerm)
            {
                batteryBackground.Fill = new SolidColorBrush(Colors.Red);
                batteryBackground.Stroke = batteryBackground.Fill;
            }
            else
            {
                batteryBackground.Fill = new SolidColorBrush(Colors.Green);
                batteryBackground.Stroke = batteryBackground.Fill;
            }
        }

        private void CheckPowerSource(PowerEventArgs e)
        {
            //TODO: Bug?
            //switch (PowerManager.PowerSource)
            //{
            //    case Microsoft.WindowsAPICodePack.ApplicationServices.PowerSource.Ups:
            //        batterySymbol.Visibility = System.Windows.Visibility.Collapsed;
            //        uspSymbol.Visibility = System.Windows.Visibility.Visible;
            //        break;

            //    case Microsoft.WindowsAPICodePack.ApplicationServices.PowerSource.Battery:
            //        batterySymbol.Visibility = System.Windows.Visibility.Visible;
            //        uspSymbol.Visibility = System.Windows.Visibility.Collapsed;
            //        break;

            //    case Microsoft.WindowsAPICodePack.ApplicationServices.PowerSource.AC:
            //        break;

            //    default:
            //        break;
            //}
        }
    }
}
